package com.cti.commons.delayqueue.repository;

import java.io.Serializable;
import java.time.Duration;
import java.util.Date;

import org.springframework.data.mongodb.core.mapping.Document;

import com.cti.commons.delayqueue.repository.constant.AbstractMessageLog;
import com.cti.commons.delayqueue.repository.constant.MsgSendStatus;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author et.wang
 * @description: 发送消息日志
 * @date 2022/11/15 14:33
 */
@Data
@Document
@NoArgsConstructor
public class DelaySendMessageLog extends AbstractMessageLog implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long expiryDays = 90L;

    public DelaySendMessageLog(String id, String topic, String message, String bizId) {
        this.messageId = (id == null ? RandomUtil.randomInt() + "" : id);
        this.topic = topic;
        this.content = message;
        this.businessId = bizId;
        this.status = MsgSendStatus.UN_SEND.getStatus();
        this.createdTime = DateUtil.formatDateTime(new Date());
        this.updatedTime = DateUtil.formatDateTime(new Date());
        this.expiryTime = new Date(System.currentTimeMillis() + Duration.ofDays(expiryDays).toMillis());
    }

    @Override
    public void success() {
        this.status = MsgSendStatus.SEND_SUCCESS.getStatus();
    }

    @Override
    public void fail() {
        this.status = MsgSendStatus.SEND_FAIL.getStatus();
    }

}
